<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script src="//cdn.66173.cn/www/js/jquery-1.8.3.min.js"></script>
<script src="http://passport.cnblogs.com/scripts/jsencrypt.min.js"></script>
<script src="AES.js"></script>
<script src="sha256.js"></script>
<script src="ZeroPadding.js"></script>
<script  src="base64.js"></script>
<body>
<textarea id="pubkey" rows="15" cols="65">
                MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDbrIgHK8qkz5IfK/A7At4SVZQ
                31TalDPsc4vzeDVjd5ao46hcf+eOEQNm8jmxxHTm6WPSTy7RDVXG/NI489L9okkd
                K++kVh2Z9GjBo5jw/n9EYojt8aYyEOc6cMHT2Fv+1smG+X/W2HeXXoJJjcFLSjBe
                CKx1SoCD4+B2ZiDQ8wIDAQAB
            </textarea><br />
姓名：<input type="text" id="name"  onchange="change()"><br/>
密码：<input type="password" id="password"  onchange="change()"><br/>
<button id="testme" onchange="data()">提交</button>
</body>
<script type="text/javascript">
    // 使用jsencrypt类库加密js方法，
    function encryptRequest(reqUrl, data, publicKey) {
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(publicKey);
        // ajax请求发送的数据对象
        var sendData = new Object();
        // 将data数组赋给ajax对象
        for (var key in data) {
            sendData[key] = encrypt.encrypt(data[key]);
            console.log(sendData[key])
        }
        $.ajax({
            url: reqUrl,
            type: 'post',
            data: sendData,
            dataType: 'json',
            //contentType: 'application/json; charset=utf-8',
            success: function (data) {
                console.info(data);
            },
            error: function (xhr) {
                //console.error('出错了');
            }
        });

    }
    var encrypt = new JSEncrypt();
    // console.log("加密后的公钥是"+encrypt.setPublicKey($('#pubkey').val()));
    var yyy="p";
    console.log("rsa加密后的结果是"+encrypt.encrypt(yyy));
   function change(){
           var data={};
           data.user=$('#name').val();
           data.pwd =$('#password').val();

   }
    $('#testme').click(function () {
        var data={};
        data.user=$('#name').val();
        data.pwd =$('#password').val();
        data.name_ =$('#password').val();
        console.log( JSON.stringify(data))
        console.log(AESEncrypt(data))
        console.log("用户名加密"+AESEncrypt(data.user))
        console.log("密码加密"+AESEncrypt(data.pwd))
        console.log("名字加密"+AESEncrypt(data.pwd))
        var str=data.user+"&"+data.pwd+"&"+data.name_
        console.log("全部数据加密"+AESEncrypt(JSON.stringify(data)))
        console.log("全部数据拼接字符串加密"+AESEncrypt(JSON.stringify(str)))
    });
</script>
<script>

    /**
     * aes加密方法========================================
     * @param data  明文
     * @returns  密文
     */
    function AESEncrypt(data){
        //密钥
        var key =  "fg5tred4qaedasds";
        var block = 16;
        var dataBase64 = Base64.encode(data).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");

        var length = dataBase64.length % block;
        var pad = block - length;
        dataBase64 += new Array( pad + 1 ).join(  String.fromCharCode(pad) );
        key  = CryptoJS.enc.Hex.parse( CryptoJS.SHA256(key).toString() );
        var iv   = CryptoJS.enc.Latin1.parse('');
        return  CryptoJS.AES.encrypt(dataBase64, key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding}).toString();
    }
    var ddd="ssssssss";
    console.log("aes加密后"+AESEncrypt(ddd))
</script>
<script>
    $(document).ready(function () {
        AESDecrypt()
    })
    /**
     * ======================解密方法=================
     * @param data 密文
     * @returns  明文
     */
    function AESDecrypt(data){
        //密钥
        var key =  "fg5tred4qaedasds";
        key  = CryptoJS.enc.Hex.parse( CryptoJS.SHA256( key ).toString() );
        var iv   = CryptoJS.enc.Latin1.parse('');
        var decrypted = CryptoJS.AES.decrypt(data, key, {iv:iv,padding:CryptoJS.pad.ZeroPadding});
        var dataBase64 =  decrypted.toString(CryptoJS.enc.Utf8);
        dataBase64 = dataBase64.replace(/-/g, "+").replace(/_/g, "/");
        var length = dataBase64.length;
        var slast = dataBase64.charAt(length-1).charCodeAt();
        return Base64.decode( dataBase64.substr(0, length-slast) );
    }
    // var str="eyJjb2RlIjowLCJtc2ciOiJcdTU1NDZcdTU0YzFcdTdjN2JcdTU3OGJcdTk1MTlcdThiZWZcdWZmMGNcdThiZjdcdTkxY2RcdTY1YjBcdTRlMGJcdTUzNTUifQ=="
      var sss={"code":0,"msg":"\u5546\u54c1\u7c7b\u578b\u9519\u8bef\uff0c\u8bf7\u91cd\u65b0\u4e0b\u5355"}
    console.log("解密后"+JSON.stringify(sss))

</script>
</html>